<?php
require 'model-db-connection/config.php';
$config = require 'model-db-connection/qrc_conf.properties.php';
?>
<div class="innerLR" style="width:1000px">
    <a href="index-mainpage.php">Main Page</a> -> Project <br/>
    <div class="widget">
        <div class="widget-head">
            <h4 class="heading">Search Project (ค้นหาโครงการ)</h4>
            <div class="clearfix"></div>
        </div>
        <div class="widget-body innerAll inner-2x">

            <table width="100%">
                <tr>
                    <td align="right" style="width:250px">Project Code (หมายเลขโครงการ):</td>
                    <td align="left" style="width:250px"><input type="text" class="form-control" id="project_code_search"></td>
                    <td align="right" style="width:250px">Project Name (โครงการ):</td>
                    <td align="left" style="width:250px"><input type="text" class="form-control" id="project_name_search"></td>
                </tr>
                <tr>
                    <td align="right" style="width:250px">Project Type (ประเภทโครงการ):</td>
                    <td align="left" style="width:250px">
                        <select class="form-control" id="project_type_search" name="project_type_search">
                            <option value=""></option>
                            <?php
                            $sqlSelectProjectType = "SELECT * FROM PROJECT_TYPE;";
                            $resultSet = mysql_query($sqlSelectProjectType);
                            while ($row = mysql_fetch_array($resultSet)) {
                                echo '<option value="' . $row['project_type_id'] . '">' . $row['project_type_name'] . '</option>';
                            }
                            ?>                                           
                        </select></td>
                    <td align="right" style="width:250px">Project Status (สถานะโครงการ):</td>
                    <td align="left" style="width:250px">
                        <select class="form-control" id="project_status_search" name="project_status_search">
                            <option value=""></option>
                            <?php
                            $sqlSelectProjectType = "SELECT * FROM PROJECT_STATUS;";
                            $resultSet = mysql_query($sqlSelectProjectType);
                            while ($row = mysql_fetch_array($resultSet)) {
                                echo '<option value="' . $row['project_status_id'] . '">' . $row['project_status_name'] . '</option>';
                            }
                            ?>                                           
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="width:250px">Project Owner (เจ้าของโครงการ):</td>
                    <td align="left" style="width:250px">
                        <select class="form-control" id="project_owner_search" name="project_owner_search">
                            <option value=""></option>
                            <?php
                            $sqlSelectProjectType = "SELECT * FROM PROJECT_OWNER;";
                            $resultSet = mysql_query($sqlSelectProjectType);
                            while ($row = mysql_fetch_array($resultSet)) {
                                echo '<option value="' . $row['project_owner_id'] . '">' . $row['project_owner_name'] . '</option>';
                            }
                            ?>                                           
                        </select>
                    </td>
                    <td align="right" style="width:250px">Customer Name (ชื่อลูกค้า):</td>
                    <td align="left" style="width:250px">
                        <select class="form-control" id="project_customer_search" name="project_customer_search">
                            <option value=""></option>
                            <?php
                            $sqlSelectProjectType = "SELECT * FROM QRC_CUSTOMER_NAME;";
                            $resultSet = mysql_query($sqlSelectProjectType);
                            while ($row = mysql_fetch_array($resultSet)) {
                                echo '<option value="' . $row['customer_id'] . '">' . $row['customer_name'] . '</option>';
                            }
                            ?>                                           
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="width:250px">Start Date (วันเริ่มต้น):</td>
                    <td align="left" style="width:250px"><input type="text" class="form-control search_date" id="start_search_date" data-date-format="yyyy-mm-dd"></td>
                    <td align="right" style="width:250px">End Date (วันสิ้นสุด):</td>
                    <td align="left" style="width:250px"><input type="text" class="form-control search_date" id="end_search_date" data-date-format="yyyy-mm-dd"></td>
                </tr>
                <tr>
                    <td style="width:250px;" colspan="4" align="center"><span style="color: red;">*ถ้าต้องการค้นหาทั้งหมด กรุณาเว้นช่องว่าง</span></td>
                </tr>
                <tr>
                    <td align="right" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="project_search_btn">Search (ค้นหา)</button></td>
                    <td align="left" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="project_reset_btn">Reset (รีเซต)</button></td>

                </tr>
            </table>

        </div>
    </div>
</div>
<div class="innerLR" style="width:1200px">

    <div id="load_project"></div>
    <div id="load_project_order"></div>

</div>
<script src="assets/components/plugins/preload/pace/pace.min.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/plugins/preload/pace/preload.pace.init.js?v=v1.0.2&sv=v0.0.1"></script>



<script type="text/javascript">
    var createOrEditState = "Create";
    $(document).ready(function() {
        $(".search_date").datepicker();

        $("#project_search_btn").click(function() {
            var projectCodeSearch = $("#project_code_search").val();
            var projectNameSearch = $("#project_name_search").val();
            var projectTypeSearch = $("#project_type_search").val();
            var projectStatusSearch = $("#project_status_search").val();
            var projectOwnerSearch = $("#project_owner_search").val();
            var projectCustomerSearch = $("#project_customer_search").val();
            var startSearchDate = $("#start_search_date").val();
            var endSearchDate = $("#end_search_date").val();
            if (!$.trim(projectCodeSearch).length &&
                    !$.trim(projectNameSearch).length &&
                    !$.trim(projectTypeSearch).length &&
                    !$.trim(projectStatusSearch).length &&
                    !$.trim(projectOwnerSearch).length &&
                    !$.trim(projectCustomerSearch) &&
                    !$.trim(startSearchDate).length &&
                    !$.trim(endSearchDate).length) {
                $("#load_project").load("ProjectPage.php?search_condition=search_all");
            } else {
                if (startSearchDate != "" && endSearchDate == "") {
                    alert("Please enter end date !!");
                }
                else if (startSearchDate == "" && endSearchDate != "") {
                    alert("Please enter start date !!");
                }
                else {
                    var dateAr = endSearchDate.split('-');
                    endSearchDate = dateAr[0] + '-' + dateAr[1] + '-' + (parseInt(dateAr[2]) + 1);

                    $("#load_project").load("ProjectPage.php?search_condition=search_criteria&projectCodeSearch=" + projectCodeSearch + "&projectNameSearch=" + projectNameSearch + "&projectTypeSearch=" + projectTypeSearch + "&projectStatusSearch=" + projectStatusSearch + "&projectOwnerSearch=" + projectOwnerSearch + "&projectCustomerSearch=" + projectCustomerSearch + "&startSearchDate=" + startSearchDate + "&endSearchDate=" + endSearchDate);
                }
            }


        });
        $("#project_reset_btn").click(function() {
            $("#load_project").empty();
            var projectCodeSearch = $("#project_code_search").val("");
            var projectNameSearch = $("#project_name_search").val("");
            var projectTypeSearch = $("#project_type_search").val("");
            var projectStatusSearch = $("#project_status_search").val("");
            var projectOwnerSearch = $("#project_owner_search").val("");
            var projectCustomerSearch = $("#project_customer_search").val("");
            var startSearchDate = $("#start_search_date").val("");
            var endSearchDate = $("#end_search_date").val("");
        });
    });

</script>